<?php

use Sdk\lib\Format;

define('FROM_HTTP', 1);

class Apis {

	public function run() {
		$raw = file_get_contents('php://input');
		$params = json_decode($raw, 1);
		$sign = $params['sign'];
		unset($params['sign']);

		$c = new Controller();

		if (Lib\Apis::GetSign($params) != $sign) {
			$c->errorMessage('sign is error');
		}

		define('PLUGIN_APPID', $params['appid']);
		$GLOBALS['witPid'] = $params['pid'];

		list($plugin, $identifier, $controller, $action) = Format::splitName($params['func']);

		define('PLUGIN_ROOT_PATH', ROOT_PATH . '/www/' . $plugin . '/');
		define('PLUGIN_PATH', PLUGIN_ROOT_PATH . $identifier . '/');

		//调用方法
		try {
			$return = call_user_func_array(
				array('Lib\Apis', $params['func']),
				$params['param']
			);
		} catch (Exception $e) {
			$c->errorMessage($e->getMessage(), $e->getCode());
		}
		$c->showMessage('', 0, $return);
	}
}